var express = require('express');
var router = express.Router();
const db = require("../database/db");

// 全部帖子


router.post('/all', function (req, res, next) {
    let usid = req.body.textid;
    console.log(usid);
    console.log(1111);
    let sql = `SELECT * FROM text WHERE  textid=?`;
     db.query(sql, [usid], function (err, result) {
       if (err) {
         console.log(err);
         res.json({ code: 400, msg: "查询失败", data:err });
       } else {
        // console.log(111);
        
         console.log(result);

         res.json({ code: 200, msg: "查询成功", data:result });
       }
     });
})

//添加帖子
router.post('/add', function (req, res, next) {
    const {textid, nei }= req.body;
    if (!textid || !nei ) {
        res.json({
            code: 400,
            msg: '参数错误'
        })
        return;
    }
    const sql = `INSERT INTO text (textid, nei) VALUES (?, ?)`;
    db.query(sql, [textid, nei], function (err, result) {
        if (err) {
            res.json({
                code: 400,
                msg: '添加错误'
            })
            return;
        }
        res.json({
            code: 200,
            msg: '添加成功'
        })
    })
})

// 删除帖子
router.post('/del', function (req, res, next) {
    const { id } = req.body;
    if (!id) {
        res.json({
            code: 400,
            msg: '参数错误'
        })
        return;
    }
    const sql = `DELETE FROM text WHERE id = ?`;
    db.query(sql, [id], function (err, result) {
        if (err) {
            res.json({
                code: 400,
                msg: '删除错误'
            })
            return;
        }else{
            res.json({
                code: 200,
                msg: '删除成功'
            })
        }
    })
})











module.exports = router;